<?php

namespace app\admin\controller;
use app\common\model\Config as ConfigModel;
/**
 * 配置控制器
 * Class Config
 * @package app\index\controller
 */
class Config extends Base
{
    /**
     * [index  获取配置参数]
     * @author [C.]
     * @return mixed
     */
    public function getIndex() {
        $configModel = new ConfigModel();
        $list = $configModel->getAllConfig();
        $config = [];
        foreach ($list as $k => $v) {
            $config[trim($v['cname'])] = $v['cvalue'];
        }

        writelog('查看配置');
        return admin_json(1,'获取配置成功',$config);
    }
    /**
     * [getOne  获取单个配置参数]
     * @author [C.]
     * @return mixed
     */
    public function getOne() {
        $param = input('param.');
        $model = new ConfigModel;
        $config = $model->findData($param['cname'],'cname','id,cvalue,content');
        return admin_json(1,'获取成功',$config);
    }
    /**
     * [postEdit  修改配置参数]
     * @author [C.]
     * @return mixed
     */
    public function postEdit() {
        $param = input('param.');
        $model = new ConfigModel;
        $model->editData($param);
        return admin_json(1,'修改成功');
    }
    /**
     * [save  批量保存配置]
     * @author [C.]
     * @param $config
     */
    public function postSave(){
        $config = input('post');

        $configModel = new ConfigModel();
        $a = array();
        if($config && is_array($config)){
            foreach ($config as $name => $value) {
                $map = array('cname' => $name);
                $configModel->SaveConfig($map,$value);
            }
        }
        writelog('修改配置');

        return admin_json(1,'保存成功！',$a);
    }


    /**
     * [sendEmail  发送测试邮件]
     * @author [C.]
     * @throws \PHPMailer\PHPMailer\Exception
     * @throws \phpmailerException
     */
    public function sendEmail(){
        $email = input('post.test_eamil');
        $res = send_email($email,'后台测试','测试发送验证码:'.mt_rand(10000,99999));
        writelog('发送测试邮件');
        exit(json_encode($res));
    }

}